package com.atgugu.realtime.util;

import com.atgugu.realtime.bean.OrderInfo;
import org.apache.commons.beanutils.BeanUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/12/27 11:00
 */
public class JdbcUtil {
    public static Connection getJdbcConnection(String driver, String url) throws ClassNotFoundException, SQLException {
        Class.forName(driver);  // 加载驱动
        return DriverManager.getConnection(url);
    }
    
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        /*List<JSONObject> list = queryList(getJdbcConnection(Constant.PHOENIX_DRIVER, Constant.PHOENIX_URL),
                                          "select * from dim_user_info where id=?",
                                          new Object[]{"392"},
                                          JSONObject.class);
        for (JSONObject obj : list) {
            System.out.println(obj);
        }*/
    
        List<OrderInfo> list = queryList(getJdbcConnection("com.mysql.cj.jdbc.Driver", "jdbc:mysql://hadoop162:3306/gmall2021?user=root&password=aaaaaa"),
                                         "select * from order_info",
                                         new Object[]{},
                                         OrderInfo.class);
        for (OrderInfo obj : list) {
            System.out.println(obj);
        }
    }
    
    //TODO
    public static <T> List<T> queryList(Connection conn,
                                        String sql,
                                        Object[] args,
                                        Class<T> tClass) {
        
        ArrayList<T> result = new ArrayList<>();
        
        // 执行sql语句
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            
            ResultSet resultSet = ps.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            while (resultSet.next()) {
                // 获取每行省数据
                T t = tClass.newInstance();  // 创建T类型的对
                // 每一列对应着T的一个属性
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    // 遍历每一列 列名对应着T的属性名
                    String columnName = metaData.getColumnLabel(i);
                    Object value = resultSet.getObject(i);
                    BeanUtils.setProperty(t, columnName, value);
                    
                }
                result.add(t);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        return result;
        
    }
}
/*
id   name  age
1    abc   10
2    aaa   20
....

 */